#include "MatchProcParamCollectRequest.h"

MatchProcParamCollectRequest::MatchProcParamCollectRequest() {}

MatchProcParamCollectRequest::MatchProcParamCollectRequest(QString factoryNo, QString transId, QDateTime eventTime, QString eventUser, QString machineNo,QString workStep,QString location, QList<MatchProcParamCollectRequestParamInfo> paramInfo):
    factoryNo(factoryNo),
    transId(transId),
    eventTime(eventTime),
    eventUser(eventUser),
    machineNo(machineNo),
    workStep(workStep),
    location(location),
    paramInfo(paramInfo)
{

}

QByteArray MatchProcParamCollectRequest::toJson() const
{
    QJsonObject json;
    int randomNumber = QRandomGenerator::global()->bounded(10000,100000);
    QString dateTime = QDateTime::currentDateTime().toString("yyyyMMddhhmmssSSS");

    json["factoryNo"] = factoryNo;
    json["transId"] = QString("%1%2").arg(dateTime).arg(randomNumber);
    json["eventTime"] = eventTime.toString("yyyy-mm-dd hh:mm:ss");
    json["eventUser"] = eventUser;
    json["machineNo"] = machineNo;
    json["workStep"] = workStep;
    json["location"] = location;

    QJsonArray paramInfoArray;
    for(const auto& infoItem:paramInfo)
    {
        QJsonObject infoObject;
        infoObject["pcu"] = infoItem.pcu;

        QJsonArray paramListArray;
        for(const auto& listItem:infoItem.paramList)
        {
            QJsonObject listObject;
            listObject["k"] = listItem.paramKey;
            listObject["v"] = listItem.paramValue;
            listObject["seq"] = listItem.seq;
            listObject["location"] = listItem.location;
            paramListArray.append(listObject);
        }
        infoObject["paramList"] = paramListArray;
        paramInfoArray.append(infoObject);
    }
    json["paramInfo"] = paramInfoArray;

    return QJsonDocument(json).toJson();
}

QString MatchProcParamCollectRequest::getEndPoint() const
{
    return "/api/getech-mes/IotController/MachProcParamCollect";
}
